package com.jinyi.wwdeng.ui.user.impl

import android.content.Context
import android.os.Handler
import com.jinyi.base.mvp.BasePresenter
import com.jinyi.base.utils.log.HHLog
import com.jinyi.wwdeng.ui.user.contract.UserFeedbackContract
import org.json.JSONObject

/**
 * JinYiTest
 *
 * @Author：wwdeng
 * @CreateTime：2024/4/26 18:45
 * <pre>
 * Presenter 实现类
</pre> *
 */
class UserFeedbackPresenterImpl : BasePresenter<UserFeedbackContract.View?, UserFeedbackModelImpl?>(), UserFeedbackContract.Presenter {

    override fun createModel(): UserFeedbackModelImpl {
        return UserFeedbackModelImpl(this)
    }

    /**
     * 获取意见反馈问题类型列表
     */
    fun loadQuestionTypeListAction(){
        if (isViewDestory()) {
            return
        }
        model!!.loadQuestionTypeListAction()
    }

    /**
     * 获取联系方式列表
     */
    fun loadContactListAction(){
        if (isViewDestory()) {
            return
        }
        model!!.loadContactListAction()
    }

    /**
     * 图片上传
     */
    fun uploadImageAction(imgPath: String){
        if (isViewDestory()) {
            return
        }
        model!!.uploadImageAction(imgPath)
    }

    /**
     * 提交意见反馈
     */
    fun submitFeedbackAction(params: JSONObject){
        if (isViewDestory()) {
            return
        }
        mvpView!!.showLoading("")
        model!!.submitFeedbackAction(params)
    }

    /**
     * 设置反馈类型列表
     */
    override fun setQuestionTypeListData(typeList: ArrayList<JSONObject>) {
        if (isViewDestory()) {
            return
        }
        mvpView!!.showQuestionTypeList(typeList)
    }

    /**
     * 设置联系方式列表
     */
    override fun setContactListData(contactList: ArrayList<JSONObject>) {
        if (isViewDestory()) {
            return
        }
        mvpView!!.showContactList(contactList)
    }

    /**
     * 图片上传完成
     */
    override fun setUploadImageSuccess(data: JSONObject) {
        if (isViewDestory()) {
            return
        }
        when(data.optInt("code")){
            0 -> {//成功
                HHLog.e("图片上传成功：$data")
                val imgUrl = data.optJSONObject("obj")!!.optString("url")
                mvpView!!.showUploadImageSuccess(imgUrl)
            }
            else -> {
            }
        }
    }

    /**
     * 意见反馈提交成功
     */
    override fun onSubmitFeedbackSuccess(data: JSONObject) {
        if (isViewDestory()) {
            return
        }
        val code = data!!.optInt("code")
        val msg = data.optString("msg")
        mvpView!!.showToast(msg)
        when(code){
            0 -> {//成功
                Handler().postDelayed({
                    mvpView!!.showSubmitSuccess()
                },500)
            }
        }
    }

    override fun onRequestError(errorMsg: String) {
        super.onRequestError(errorMsg)
        if (isViewDestory()) {
            return
        }
        mvpView!!.showToast(errorMsg)
    }

    override fun onRequestFinish() {
        super.onRequestFinish()
        if (isViewDestory()) {
            return
        }
        mvpView!!.hideLoading()
    }

    override val context: Context?
        get() = if (!isViewDestory()) {
            mvpView!!.getContext()
        } else null
}
